package com.tangkun.environment.network;

import android.text.TextUtils;

import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.tangkun.environment.application.BaseApplication;
import com.tangkun.environment.utils.SharePrefercesUtils;

import java.io.IOException;

/**
 * Created Time: 2017/3/18
 * <p/>
 * Author:tangkun
 * <p/>
 * 功能：Token拦截器
 */
public class TokenInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        //本地存储的token 登录和未登录用户都会有一个token，登录的用户会刷新这个token
        String token = SharePrefercesUtils.getToken(BaseApplication.appContext);
        if (!TextUtils.isEmpty(token)) {
            request = request.newBuilder().addHeader("authmark", token).build();
        } else {
            request = request.newBuilder().addHeader("authmark", "cUJYWmt1d3Q2RWNsNFdueGs0M2laSXEwa1dkRnV2ajhkbGRBWE9GVnNXTncyblRDZHdVY0EvSXNCaXpVbkJmNFZoQVRoUHI0bUU4azRKTDZjdGhCbHRaSWFrbVFVQlM1a3Y5dS9oZHJ6K2Nrd0ZJR2dCdncxMjZnYkE2bmtDcUxUYXl6a2hFVlRiU0RYYituQSs1WUNtem5JZ2tTTGlVOUVhTG05dWhJVUxSU2N0a1NoMGt4dGhMbFViV1hoUlVmd1ZjRWV2TGdOQlpIVHIwZ2xmcUNsNnVILzVITzJLU3pOTWRmMTVWcDl0dWErRWpIUk03RGppREZuTFRTd2FXL0poUmJkaEZYS3dwVkZvWE1VZkxpVkJkVjc5ckhUT1FKNU92UENVQVlpYklCb2hoT0ZBZXVkRHFoTnFJK01Xdy9MUmdtTWx0U2Q3ZUw4OVU1K0xpMmU5V2hlMEIxb0JETWYzVDc2WHMvU09GRXlka1JNbE1tOEo2VXJXQUMzWGxO").build();
        }
        Response response = chain.proceed(request);
        return response;
    }

}
